package com.mx.web;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query;

import com.alibaba.fastjson.JSON;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mx.web.base.tool.SimplePropertyPreFilter;
import com.mx.web.model.Article;
import com.mx.web.model.Authority;
import com.mx.web.model.User;

public class MongoApp {

	private static final Logger logger = LoggerFactory.getLogger(MongoApp.class);

  	public static void main(String[] args) throws Exception {

	  	int port = 27017;
		String host = "localhost";
		String user = "imxood";
		String password = "imx123";
		String database = "imxood";
		String authenticationDatabase = "admin";

		List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
		MongoCredential userCredentials = MongoCredential.createCredential(user, authenticationDatabase, password.toCharArray());
		credentialsList.add(userCredentials);

		MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(new ServerAddress(host, port), credentialsList), database);

		MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
		mongoTemplate.dropCollection(User.class);

		//添加一个用户
		User user1 = new User("imxood", "imx123", "ADMIN");
		user1.addAuthority("USER");
		mongoTemplate.insert(user1);
		
		Query query = new Query();
		logger.info(""+mongoTemplate.findOne(query, User.class));
		
		
		//"{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}"
		logger.info("测试BasicQuery: "+mongoTemplate.findOne(new BasicQuery("{ username : 'imxood' }"), User.class));
		
		
		Query query1 = query( where("username").is("imxood") );
		User user2 = mongoTemplate.findOne(query1, User.class);
		logger.info("测试BasicQuery: {} {}", user2.getUsername(), user2.getPassword());
		
		
		List<Article> articles = mongoTemplate.find(query, Article.class);
		System.out.println("size: "+articles.size());
		
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
		filter.setExcludes(new HashMap<Class<?>, String[]>() {
            
            private static final long serialVersionUID = -8411128674046835592L;
               
            {
                put(User.class, new String[] { "enabled" });
                put(Authority.class, new String[] { "authority" });
            }
        });
		
		logger.info("JSON.toJSONString:{}",JSON.toJSONString(user1,filter));
		
//		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
//		JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
  	}

}